/**
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * 本软件及相关文档文件（以下简称“软件”）的版权归 铭软科技 所有
 * 遵循铭软科技《保密协议》
 */

package net.mingsoft.mwebsite.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;

/**
 * Mybatis 拦截器配置
 */
@Configuration
public class TenantLineInnerInterceptorConfig implements WebMvcConfigurer {

    @Bean
    public MybatisPlusInterceptor mybatisPlusAppHandlerInterceptor(DataSource dataSource, @Autowired(required = false) net.mingsoft.mwebsite.handler.AppHandler appHandler) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //多租户
        if (appHandler != null) {
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(appHandler));
        }
        return interceptor;
    }

    /**
     * 增加对AppId拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 排除配置
        registry.addInterceptor(new net.mingsoft.mwebsite.interceptor.AppInterceptor()).excludePathPatterns("/static/**", "/app/**", "/webjars/**",
                "/*.html", "/*.htm");
    }
}
